<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="8" :lg="9" :md="10" :sm="24">
            <a-form-item :label="$t('创建时间')">
              <j-date
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_begin"
                style="min-width: calc(50% - 10px)"
              ></j-date>
              <span class="query-group-split-cust"></span>
              <j-date
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_end"
                style="min-width: calc(50% - 10px)"
              ></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="5" :md="7" :sm="24">
            <a-form-item :label="$t('仓库')">
              <a-tree-select
                allowClear
                v-model="queryParam.store"
                style="width: 100%"
                :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                :tree-data="storeList"
                :search-placeholder="$t('请选择')"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="6" :md="7" :sm="24">
            <a-form-item :label="$t('盘点状态')">
              <a-select v-model="queryParam.state">
                <a-select-option v-for="(item, index) in stateList" :key="index" :value="item.value">{{
                  item.title
                }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('操作员')">
                <a-select v-model="queryParam.createBy">
                  <a-select-option v-for="(item, index) in userList" :value="item.id" :key="index">{{
                    item.realname
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="5" :lg="6" :md="7" :sm="24">
              <a-form-item :label="$t('订单号')">
                <a-input v-model="queryParam.id" :placeholder="$t('请输入订单号')" />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24" v-if="!tenantid">
              <a-form-item :label="$t('门店')">
                <a-select v-model="queryParam.tenantId">
                  <a-select-option v-for="(item, i) in shops" :key="i" :value="item.value"
                    >{{ item.title }}<span v-if="item.value != ''">（{{ item.value }}）</span></a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{$t('查询')}}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{$t('重置')}}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <div class="table-operator">
      <a-button type="primary" :loading="exloading" @click="handleExportXls($t('盘点单'))" v-has="'BisGoodsInventoryController:export'">{{ $t('导出') }}</a-button>
      <a-button type="primary" @click="handleAdd" v-has="'BisGoodsInventoryController:add'">{{ $t('新增') }}</a-button>
    </div>
    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-if="record.state == '0'" v-has="'BisGoodsInventoryController:edit'">{{ $t('编辑') }}</a>
          <a-popconfirm :title="$t('确定删除吗?')" @confirm="() => handleDelete(record.id)">
            <a v-if="record.state == '0'" style="margin-left: 8px" v-has="'BisGoodsInventoryController:delete'">{{ $t('删除') }}</a>
          </a-popconfirm>
          <a @click="handleDetail(record, 1)" style="margin-left: 8px">{{ $t('详情') }}</a>
        </span>
      </a-table>
    </div>

    <!-- <goods-inventory-modal ref="modalForm" @ok="modalFormOk"></goods-inventory-modal> -->
    <goods-inventory-standby-modal ref="modalForm" @ok="modalFormOk"></goods-inventory-standby-modal>
    <stock-detail-modal ref="detailModal"></stock-detail-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import moment from 'moment'
import Vue from 'vue'
import { TENANT_ID } from '@/store/mutation-types'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { initDictOptions } from '@/components/dict/JDictSelectUtil'
import { getAction } from '@/api/manage'
/* import GoodsInventoryModal from './modules/GoodsInventoryModal' */
import GoodsInventoryStandbyModal from './modules/GoodsInventoryStandbyModal'
import StockDetailModal from './modules/StockDetailModal'

export default {
  name: 'GoodsInventoryList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    /* GoodsInventoryModal, */
    GoodsInventoryStandbyModal,
    StockDetailModal,
  },
  data() {
    return {
      moment,
      description: '商品盘点管理页面',
      queryParam: {
        store: '',
        state: '',
        createBy: '',
        tenantId:''
      },
      // 表头
      columns: [
        {
          title: this.$t('订单号'),
          align: 'center',
          dataIndex: 'id',
        },
        {
          title: this.$t('创建时间'),
          align: 'center',
          dataIndex: 'createTime',
        },
        {
          title: this.$t('操作员'),
          align: 'center',
          dataIndex: 'createBy',
        },
        {
          title: this.$t('仓库'),
          align: 'center',
          dataIndex: 'store_dictText',
        },
        {
          title: this.$t('盘点状态'),
          align: 'center',
          dataIndex: 'state_dictText',
        },
        {
          title: this.$t('备注'),
          align: 'center',
          dataIndex: 'comment',
        },
        {
          title: this.$t('操作'),
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/goods/bisGoodsInventory/list',
        delete: '/goods/bisGoodsInventory/delete',
        deleteBatch: '/goods/bisGoodsInventory/deleteBatch',
        exportXlsUrl: '/goods/bisGoodsInventory/exportXls',
        importExcelUrl: 'goods/bisGoodsInventory/importExcel',
      },
      dictOptions: {},
      superFieldList: [],
      storeList: [],
      stateList: [],
      userList: [],
      shops: [],
      tenantid: Vue.ls.get(TENANT_ID),
    }
  },
  created() {
    if (!this.tenantid) {
      initDictOptions('bis_shop,name,code').then((res) => {
        if (res.success) {
          this.shops = res.result
          this.shops.unshift({ value: 0, title: this.$t('总部') })
          this.shops.unshift({ value: '', title: this.$t('全部') })
        }
      })
      this.columns.unshift({
        title: this.$t('所属门店'),
        align: 'left',
        dataIndex: 'tenantId',
        customRender: (t, r) => {
          let str
          if (!t) {
            str = this.$t('总部')
          } else {
            str = r.tenantId_dictText ? t + ' ' + r.tenantId_dictText : t
          }
          return str
        },
      })
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {
      this.getStoreList()
      initDictOptions('bis_goods_inventory.state').then((res) => {
        if (res.success) {
          this.stateList = res.result
          this.stateList.unshift({ title: this.$t('全部'), value: '' })
        }
      })
      getAction('/sys/user/list').then((res) => {
        if (res.success) {
          this.userList = res.result.records
          this.userList.forEach((col) => {
            col.id = col.realname
          })
          this.userList.unshift({ id: '', realname: this.$t('全部') })
        }
      })
    },
    getStoreList() {
      let that = this
      let storeList = []
      let param = {}
      param.pageNo = 1
      param.pageSize = 9999
      param.column = 'id'
      param.order = 'DESC'
      getAction('/goods/bisGoodsStore/rootList', param).then((res) => {
        if (res.success) {
          that.allChildKind = []
          storeList = res.result.records
          if (storeList.length > 0) {
            storeList.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.id = '' + col.id
              col.pid = '' + col.pid
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChildStore(col)
              }
            })
            that.storeList = that.getDataByResult(storeList)
            that.storeList.unshift({ key: '', id: '', value: '', title: that.$t('全部') })
          }
        }
      })
    },
    getHasChildStore(store) {
      let that = this
      let query = {}
      query.pageNo = 1
      query.pageSize = 9999
      query.pid = store.id
      getAction('/goods/bisGoodsStore/rootList', query).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.pid = '' + col.pid
              col.id = '' + col.id
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChildStore(col)
              }
            })
            store.children = that.getDataByResult(res.result.records)
            that.storeList = [...that.storeList]
          }
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    getDataByResult(result) {
      return result.map((item) => {
        return item
      })
    },
    handleDetail(record, state) {
      this.$refs.modalForm.edit(record, state)
      this.$refs.modalForm.title = this.$t('详情')
      this.$refs.modalForm.disableSubmit = true
    },
    searchReset() {
      this.queryParam = {
        store: '',
        state: '',
        createBy: '',
        tenantId:''
      }
      this.searchQuery()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>